home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / casetab.c < prev    next >
C/C++ Source or Header  |  1993-10-06  |  8KB  |  254 lines

  1. /* GNU Emacs routines to deal with case tables.
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Written by Howard Gayle.  See chartab.c for details. */
  21.  
  22. #include "config.h"
  23. #include "lisp.h"
  24. #include "buffer.h"
  25.  
  26. #include "casetab_p.h"
  27. static Lisp_Object check_case_table _P_((Lisp_Object obj));
  28. static Lisp_Object set_case_table _P_((Lisp_Object table, int standard));
  29.  
  30. Lisp_Object Qcase_table_p;
  31. Lisp_Object Vascii_downcase_table, Vascii_upcase_table;
  32. Lisp_Object Vascii_canon_table, Vascii_eqv_table;
  33.  
  34. DEFUN ("case-table-p", Fcase_table_p, Scase_table_p, 1, 1, 0,
  35.   "Return t iff ARG is a case table.\n\
  36. See `set-case-table' for more information on these data structures.")
  37.   (table)
  38.      Lisp_Object table;
  39. {
  40.   Lisp_Object down, up, canon, eqv;
  41.   down = Fcar_safe (table);
  42.   up = Fcar_safe (Fcdr_safe (table));
  43.   canon = Fcar_safe (Fcdr_safe (Fcdr_safe (table)));
  44.   eqv = Fcar_safe (Fcdr_safe (Fcdr_safe (Fcdr_safe (table))));
  45.  
  46. #define STRING256_P(obj) \
  47.   (XTYPE (obj) == Lisp_String && XSTRING (obj)->size == 256)
  48.  
  49.   return (STRING256_P (down)
  50.       && (NILP (up) || STRING256_P (up))
  51.       && ((NILP (canon) && NILP (eqv))
  52.           || (STRING256_P (canon) && STRING256_P (eqv)))
  53.       ? Qt : Qnil);
  54. }
  55.  
  56. static Lisp_Object
  57. check_case_table (obj)
  58.      Lisp_Object obj;
  59. {
  60.   register Lisp_Object tem;
  61.  
  62.   while (tem = Fcase_table_p (obj), NILP (tem))
  63.     obj = wrong_type_argument (Qcase_table_p, obj);
  64.   return (obj);
  65. }   
  66.  
  67. DEFUN ("current-case-table", Fcurrent_case_table, Scurrent_case_table, 0, 0, 0,
  68.   "Return the case table of the current buffer.")
  69.   ()
  70. {
  71.   Lisp_Object down, up, canon, eqv;
  72.   
  73.   down = current_buffer->downcase_table;
  74.   up = current_buffer->upcase_table;
  75.   canon = current_buffer->case_canon_table;
  76.   eqv = current_buffer->case_eqv_table;
  77.  
  78.   return Fcons (down, Fcons (up, Fcons (canon, Fcons (eqv, Qnil))));
  79. }
  80.  
  81. DEFUN ("standard-case-table", Fstandard_case_table, Sstandard_case_table, 0, 0, 0,
  82.   "Return the standard case table.\n\
  83. This is the one used for new buffers.")
  84.   ()
  85. {
  86.   return Fcons (Vascii_downcase_table,
  87.         Fcons (Vascii_upcase_table,
  88.                Fcons (Vascii_canon_table,
  89.                   Fcons (Vascii_eqv_table, Qnil))));
  90. }
  91.  
  92. DEFUN ("set-case-table", Fset_case_table, Sset_case_table, 1, 1, 0,
  93.   "Select a new case table for the current buffer.\n\
  94. A case table is a list (DOWNCASE UPCASE CANONICALIZE EQUIVALENCES)\n\
  95.  where each element is either nil or a string of length 256.\n\
  96. DOWNCASE maps each character to its lower-case equivalent.\n\
  97. UPCASE maps each character to its upper-case equivalent;\n\
  98.  if lower and upper case characters are in 1-1 correspondence,\n\
  99.  you may use nil and the upcase table will be deduced from DOWNCASE.\n\
  100. CANONICALIZE maps each character to a canonical equivalent;\n\
  101.  any two characters that are related by case-conversion have the same\n\
  102.  canonical equivalent character.\n\
  103. EQUIVALENCES is a map that cyclicly permutes each equivalence class\n\
  104.  (of characters with the same canonical equivalent).\n\
  105. Both CANONICALIZE and EQUIVALENCES may be nil, in which case\n\
  106.  both are deduced from DOWNCASE and UPCASE.")
  107.   (table)
  108.      Lisp_Object table;
  109. {
  110.   return set_case_table (table, 0);
  111. }
  112.  
  113. DEFUN ("set-standard-case-table", Fset_standard_case_table, Sset_standard_case_table, 1, 1, 0,
  114.   "Select a new standard case table for new buffers.\n\
  115. See `set-case-table' for more info on case tables.")
  116.   (table)
  117.      Lisp_Object table;
  118. {
  119.   return set_case_table (table, 1);
  120. }
  121.  
  122. static Lisp_Object
  123. set_case_table (table, standard)
  124.      Lisp_Object table;
  125.      int standard;
  126. {
  127.   Lisp_Object down, up, canon, eqv;
  128.  
  129.   check_case_table (table);
  130.  
  131.   down = Fcar_safe (table);
  132.   up = Fcar_safe (Fcdr_safe (table));
  133.   canon = Fcar_safe (Fcdr_safe (Fcdr_safe (table)));
  134.   eqv = Fcar_safe (Fcdr_safe (Fcdr_safe (Fcdr_safe (table))));
  135.  
  136.   if (NILP (up))
  137.     {
  138.       up = Fmake_string (make_number (256), make_number (0));
  139.       compute_trt_inverse (XSTRING (down)->data, XSTRING (up)->data);
  140.     }
  141.  
  142.   if (NILP (canon))
  143.     {
  144.       register int i;
  145.       unsigned char *upvec = XSTRING (up)->data;
  146.       unsigned char *downvec = XSTRING (down)->data;
  147.  
  148.       canon = Fmake_string (make_number (256), make_number (0));
  149.       eqv = Fmake_string (make_number (256), make_number (0));
  150.  
  151.       /* Set up the CANON vector; for each character,
  152.      this sequence of upcasing and downcasing ought to
  153.      get the "preferred" lowercase equivalent.  */
  154.       for (i = 0; i < 256; i++)
  155.     XSTRING (canon)->data[i] = downvec[upvec[downvec[i]]];
  156.  
  157.       compute_trt_inverse (XSTRING (canon)->data, XSTRING (eqv)->data);
  158.     }
  159.  
  160.   if (standard)
  161.     {
  162.       Vascii_downcase_table = down;
  163.       Vascii_upcase_table = up;
  164.       Vascii_canon_table = canon;
  165.       Vascii_eqv_table = eqv;
  166.     }
  167.   else
  168.     {
  169.       current_buffer->downcase_table = down;
  170.       current_buffer->upcase_table = up;
  171.       current_buffer->case_canon_table = canon;
  172.       current_buffer->case_eqv_table = eqv;
  173.     }
  174.   return table;
  175. }
  176.  
  177. /* Given a translate table TRT, store the inverse mapping into INVERSE.
  178.    Since TRT is not one-to-one, INVERSE is not a simple mapping.
  179.    Instead, it divides the space of characters into equivalence classes.
  180.    All characters in a given class form one circular list, chained through
  181.    the elements of INVERSE.  */
  182.  
  183. void
  184. compute_trt_inverse (trt, inverse)
  185.      register unsigned char *trt;
  186.      register unsigned char *inverse;
  187. {
  188.   register int i = 0400;
  189.   register unsigned char c, q;
  190.  
  191.   while (i--)
  192.     inverse[i] = i;
  193.   i = 0400;
  194.   while (i--)
  195.     {
  196.       if ((q = trt[i]) != (unsigned char) i)
  197.     {
  198.       c = inverse[q];
  199.       inverse[q] = i;
  200.       inverse[i] = c;
  201.     }
  202.     }
  203. }
  204.  
  205. _VOID_
  206. init_casetab_once ()
  207. {
  208.   register int i;
  209.   Lisp_Object tem;
  210.  
  211.   tem = Fmake_string (make_number (256), make_number (0));
  212.   Vascii_downcase_table = tem;
  213.   Vascii_canon_table = tem;
  214.  
  215.   for (i = 0; i < 256; i++)
  216.     XSTRING (tem)->data[i] = (i >= 'A' && i <= 'Z') ? i + 040 : i;
  217.  
  218.   tem = Fmake_string (make_number (256), make_number (0));
  219.   Vascii_upcase_table = tem;
  220.   Vascii_eqv_table = tem;
  221.  
  222.   for (i = 0; i < 256; i++)
  223.     XSTRING (tem)->data[i]
  224.       = ((i >= 'A' && i <= 'Z')
  225.      ? i + ('a' - 'A')
  226.      : ((i >= 'a' && i <= 'z')
  227.         ? i + ('A' - 'a')
  228.         : i));
  229. }
  230.  
  231. _VOID_
  232. syms_of_casetab ()
  233. {
  234.   Qcase_table_p = intern ("case-table-p");
  235.   staticpro (&Qcase_table_p);
  236.   staticpro (&Vascii_downcase_table);
  237.   staticpro (&Vascii_upcase_table);
  238.   staticpro (&Vascii_canon_table);
  239.   staticpro (&Vascii_eqv_table);
  240.  
  241.   defsubr (&Scase_table_p);
  242.   defsubr (&Scurrent_case_table);
  243.   defsubr (&Sstandard_case_table);
  244.   defsubr (&Sset_case_table);
  245.   defsubr (&Sset_standard_case_table);
  246.  
  247. #if 0
  248.   DEFVAR_LISP ("ascii-downcase-table", &Vascii_downcase_table,
  249.            "String mapping ASCII characters to lowercase equivalents.");
  250.   DEFVAR_LISP ("ascii-upcase-table", &Vascii_upcase_table,
  251.            "String mapping ASCII characters to uppercase equivalents.");
  252. #endif
  253. }
  254.